package com.zhengtoudai.ui.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;

import com.zhengtoudai.R;
import com.zhengtoudai.base.App;
import com.zhengtoudai.base.BaseActivity;
import com.zhengtoudai.global.Config;
import com.zhengtoudai.http.HttpUtil;
import com.zhengtoudai.injector.component.DaggerActivityComponent;
import com.zhengtoudai.injector.module.ActivityModule;
import com.zhengtoudai.ui.bean.Result;
import com.zhengtoudai.ui.presenter.ChangeLoginPresenterImpl;
import com.zhengtoudai.ui.view.CommonView;
import com.zhengtoudai.util.AbToastUtil;
import com.zhengtoudai.util.Tool;

import javax.inject.Inject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ChangeLoginPassWordActivity extends BaseActivity implements CommonView {

    @Bind(R.id.edt_old_pass_word)
    EditText mEdtOldPassWord;
    @Bind(R.id.edt_new_pass_word)
    EditText mEdtNewPassWord;
    @Bind(R.id.edt_affirm_pass_word)
    EditText mEdtAffirmPassWord;
    @Bind(R.id.btn_affirm)
    Button mBtnAffirm;

    @Inject
    ChangeLoginPresenterImpl mPresenter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_login_pass_word);
        ButterKnife.bind(this);
    }

    @Override
    protected void initTitle() {
        titleUtil.setTitle("修改登录密码");
    }

    @Override
    protected void initView() {
        DaggerActivityComponent.builder().appComponent(App.mApp.getAppComponent())
                .activityModule(new ActivityModule(this))
                .build()
                .inject(this);
        mPresenter.attachView(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mPresenter.detachView();
    }

    @OnClick(R.id.btn_affirm)
    public void onClick() {
        if (Tool.isFastDoubleClick()) return;
        mEdtOldPassWord.setError(null);
        mEdtNewPassWord.setError(null);
        mEdtAffirmPassWord.setError(null);

        String oldPassowrd=mEdtOldPassWord.getText().toString();
        String newPassword=mEdtNewPassWord.getText().toString();
        String enterPassword=mEdtAffirmPassWord.getText().toString();
        if (TextUtils.isEmpty(oldPassowrd)){
            mEdtOldPassWord.setError("请输入原密码");
            mEdtOldPassWord.requestFocus();
        }else if (TextUtils.isEmpty(newPassword)){
            mEdtNewPassWord.setError("请输入新密码");
            mEdtNewPassWord.requestFocus();
        }else if (TextUtils.isEmpty(enterPassword)){
            mEdtAffirmPassWord.setError("请再次输入新密码");
            mEdtAffirmPassWord.requestFocus();
        }else if (oldPassowrd.length()<6){
            mEdtOldPassWord.setError("密码不得小于6位数");
            mEdtOldPassWord.requestFocus();
        }else if (newPassword.length()<6){
            mEdtNewPassWord.setError("密码不得小于6位数");
            mEdtNewPassWord.requestFocus();
        }else if (enterPassword.length()<6){
            mEdtAffirmPassWord.setError("密码不得小于6位数");
            mEdtAffirmPassWord.requestFocus();
        }else if (!newPassword.equals(enterPassword)){
            mEdtAffirmPassWord.setError("两次密码输入不一致");
            mEdtAffirmPassWord.requestFocus();
        }else{
            mPresenter.changePassword(oldPassowrd,newPassword,enterPassword);
        }
    }

    @Override
    public void showDialog() {
        showProgressDialog("正在修改密码");
    }

    @Override
    public void onResult(Result result) {
        if (result.getCode()== Config.SUCCESSFUL_CODE){
            startActivity(LoginActivity.class);
            App.mApp.logout();
            finish();
        }
        AbToastUtil.showToast(this,result.getMessage());
    }

    @Override
    public void onFailure(Throwable e) {
        HttpUtil.disposeOnFailure(this,e);
    }

    @Override
    public void dismissDialog() {
        dismissProgressDialog();
    }
}
